package com.sls.game.parser;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sls.game.dao.maxshu.MaxshuAccountDAO;
import com.sls.game.dao.maxshu.MaxshuAsDAO;
import com.sls.game.dao.maxshu.MaxshuRechargeDAO;
import com.sls.game.pojo.maxshu.MaxshuAccountType;
import com.sls.game.pojo.maxshu.MaxshuAreaServer;
import com.sls.game.pojo.maxshu.MaxshuProduct;
import com.sls.game.pojo.maxshu.MaxshuRechargeType;
import com.sls.game.pojo.maxshu.MaxshuRespInfo;


public class ParseMaxshuRespInfo {

	private static Logger log = Logger.getLogger(ParseMaxshuRespInfo.class);

	@SuppressWarnings("unchecked")
	public static MaxshuRespInfo getInfoOfRecharge(String xml) {
		MaxshuRespInfo respInfo = new MaxshuRespInfo();
		Document document = null;
		Element root = null;
		// 读取XML文件,获得document对象
		try {
			document = DocumentHelper.parseText(xml);
			root = document.getRootElement();
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				Element secondEle = it.next();
				String tagName = secondEle.getName();
				if ("order-id".equals(tagName)) {
					respInfo.setMaxshuOrderId(secondEle.getTextTrim());
				} else if ("merchant-order-id".equals(tagName)) {
					respInfo.setOrderId(secondEle.getTextTrim());
				} else if ("state".equals(tagName)) {
					respInfo.setState(secondEle.getTextTrim());
				} else if ("state-info".equals(tagName)) {
					respInfo.setStateInfo(secondEle.getTextTrim());
				}
			}
		} catch (DocumentException e) {
			log.error("解析数网sup充值接口返回结果异常", e);
		}
		return respInfo;
	}

	@SuppressWarnings("unchecked")
	public static List<MaxshuProduct> getAllProduct(String xml) {
		List<MaxshuProduct> list = new ArrayList<MaxshuProduct>();
		MaxshuProduct wp = null;
		Document document = null;
		Element root = null;
		// 读取XML文件,获得document对象
		try {
			document = DocumentHelper.parseText(xml);
			root = document.getRootElement();
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				wp = new MaxshuProduct();
				Element secondEle = it.next();
				String tagName = secondEle.getName();
				if ("product".equals(tagName)) {
					Iterator<Element> it2 = secondEle.elementIterator();
					while (it2.hasNext()) {
						Element thirdEle = it2.next();
						String tagName2 = thirdEle.getName();
						if ("id".equals(tagName2)) {
							wp.setProductId(thirdEle.getTextTrim());
						} else if ("name".equals(tagName2)) {
							wp.setProductName(thirdEle.getTextTrim());
						} else if ("type".equals(tagName2)) {
							wp.setRechargeMode(thirdEle.getTextTrim());
						} else if ("par-value".equals(tagName2)) {
							wp.setProductParvalue(thirdEle.getTextTrim());
						} else if ("price".equals(tagName2)) {
							wp.setProductPrice(thirdEle.getTextTrim());
						}
					}
				}
				list.add(wp);
			}

		} catch (DocumentException e) {
			log.error("解析数网sup获取产品接口返回结果异常", e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public static MaxshuRespInfo getBalanceInfo(String xml) {
		MaxshuRespInfo respInfo = new MaxshuRespInfo();
		Document document = null;
		Element root = null;
		// 读取XML文件,获得document对象
		try {
			document = DocumentHelper.parseText(xml);
			root = document.getRootElement();
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				Element secondEle = it.next();
				String tagName = secondEle.getName();
				if ("merchant-id".equals(tagName)) {
					respInfo.setMerchantId(secondEle.getTextTrim());
				} else if ("merchant-name".equals(tagName)) {
					respInfo.setMerchantName(secondEle.getTextTrim());
				} else if ("balance".equals(tagName)) {
					respInfo.setBalance(secondEle.getTextTrim());
				} else if ("state".equals(tagName)) {
					respInfo.setState(secondEle.getTextTrim());
				}
			}
		} catch (DocumentException e) {
			log.error("解析数网sup查询商户信息接口返回结果异常", e);
		}
		return respInfo;
	}

	@SuppressWarnings("unchecked")
	public static void saveAssistInfo(String xmlStr,
			MaxshuAccountDAO MaxshuAccountDAO,
			MaxshuRechargeDAO MaxshuRechargeDAO, MaxshuAsDAO MaxshuAsDAO) {
		Document document = null;
		Element root = null;
		String goodsId = null;
		String goodsName = null;
		// 读取XML文件,获得document对象
		try {
			document = DocumentHelper.parseText(xmlStr);
			root = document.getRootElement();
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				Element secondEle = it.next();
				String secTagName = secondEle.getName();
				if ("id".equals(secTagName)) {
					goodsId = secondEle.getTextTrim();
				} else if ("name".equals(secTagName)) {
					goodsName = secondEle.getTextTrim();
				} else if ("game-list".equals(secTagName)) {
					Iterator<Element> it2 = secondEle.elementIterator();
					while (it2.hasNext()) {
						Element thirdEle = it2.next();
						String thirdTagName = thirdEle.getName();
						if ("game".equals(thirdTagName)) {
							Attribute gameName = thirdEle.attribute("name");
							Attribute gameValue = thirdEle.attribute("value");
							Iterator<Element> it3 = thirdEle.elementIterator();
							while (it3.hasNext()) {
								Element fourEle = it3.next();
								String fourTagName = fourEle.getName();
								if ("target-account-type-list"
										.equals(fourTagName)) {
									Iterator<Element> it4 = fourEle
											.elementIterator();
									while (it4.hasNext()) {
										Element fiveEle = it4.next();
										Attribute accountName = fiveEle
												.attribute("name");
										Attribute accountValue = fiveEle
												.attribute("value");
										MaxshuAccountType acc = new MaxshuAccountType();
										acc.setId(UUID.randomUUID().toString());
										acc.setProductId(goodsId);
										acc.setProductName(goodsName);
										acc.setGameName(gameName.getValue());
										acc.setGameNameValue(gameValue
												.getValue());
										acc.setAccountTypeName(accountName
												.getValue());
										acc.setAccountTypeValue(accountValue
												.getValue());
										MaxshuAccountDAO.insertAccount(acc);
									}

								} else if ("recharge-mode-list"
										.equals(fourTagName)) {
									Iterator<Element> it4 = fourEle
											.elementIterator();
									while (it4.hasNext()) {
										Element fiveEle = it4.next();
										Attribute rechargeName = fiveEle
												.attribute("name");
										Attribute rechargeValue = fiveEle
												.attribute("value");
										MaxshuRechargeType recharge = new MaxshuRechargeType();
										recharge.setId(UUID.randomUUID()
												.toString());
										recharge.setProductId(goodsId);
										recharge.setProductName(goodsName);
										recharge.setGameName(gameName
												.getValue());
										recharge.setGameNameValue(gameValue
												.getValue());
										recharge.setRechargeTypeName(rechargeName
												.getValue());
										recharge.setRechargeTypeValue(rechargeValue
												.getValue());
										MaxshuRechargeDAO
												.insertRecharge(recharge);
									}
								} else if ("area-list".equals(fourTagName)) {
									Iterator<Element> it4 = fourEle
											.elementIterator();
									while (it4.hasNext()) {
										Element fiveEle = it4.next();
										String fiveTagName = fiveEle.getName();
										if ("area".equals(fiveTagName)) {
											Attribute areaName = fiveEle
													.attribute("name");
											Attribute areaValue = fiveEle
													.attribute("value");
											Iterator<Element> it5 = fiveEle
													.elementIterator();
											while (it5.hasNext()) {
												Element sixEle = it5.next();
												String sixTagName = sixEle
														.getName();
												if ("server".equals(sixTagName)) {
													Attribute serverName = sixEle
															.attribute("name");
													Attribute serverValue = sixEle
															.attribute("value");
													MaxshuAreaServer as = new MaxshuAreaServer();
													as.setId(UUID.randomUUID()
															.toString());
													as.setProductId(goodsId);
													as.setProductName(goodsName);
													as.setGameName(gameName
															.getValue());
													as.setGameNameValue(gameValue
															.getValue());
													as.setAreaName(areaName
															.getValue());
													as.setAreaValue(areaValue
															.getValue());
													as.setServerName(serverName
															.getValue());
													as.setServerValue(serverValue
															.getValue());
													MaxshuAsDAO
															.insertAreaServer(as);
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}

		} catch (DocumentException e) {
			log.error("解析数网sup获取产品接口返回结果异常", e);
		}

	}

}
